package com.glory.cqaospda.viewmodel

import com.glory.cqaospda.bean.AccountBean
import com.glory.cqaospda.bean.MaterialListBean
import com.glory.cqaospda.model.TaskRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.CallObserveBean
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser

class SkipDialogModel : BaseViewModel(){


    private val repository by lazy { TaskRepository() }

    fun defaultLotInfo(lotID: String) {
        val map = JsonObject()
        map.addProperty("Lotid", lotID)
        map.addProperty("EquipmentName", "")
        map.addProperty("BatchLotType", "")
        launchOnlyResult({
            val defaultLotInfo =
                repository.defaultLotInfo(map,"EMS_GetLotInfo")
            defUI.callObserve.value = CallObserveBean(1, defaultLotInfo)
        })
    }

    fun defaultLotSwitch(lotID: String,checked: Boolean) {
        val map = JsonObject()
        map.addProperty("LotNo", lotID)
        map.addProperty("LotAction", if (checked) "1" else "0")
        launchOnlyResult({
            repository.defaultLotSwitch(map,"EMS_LotSwitch")
            defUI.callObserve.value = CallObserveBean(3)
        })
    }

    fun defaultEquipmentConversion(materialList: ArrayList<MaterialListBean>,eqpid:String,remark:String,buyoff:String) {
        val map = JsonObject()
        map.addProperty("LotNo","")
        map.addProperty("StepName","")
        map.addProperty("EquipmentName",eqpid)
        map.addProperty("Remark",remark)
        map.addProperty("BuyoffFlag",buyoff)
        map.addProperty("ConversionType","MaterialChange")
        map.add("MaterialList", JsonParser().parse(Gson().toJson(materialList)).asJsonArray)

        launchWithError({
            repository.defaultEquipmentConversion(map,"EMS_EquipmentConversion")
            defUI.callObserve.value = CallObserveBean(2)
        },{
            defUI.callObserve.value = CallObserveBean(1)
        })
    }

    fun defaultGetAccount(cardID: String) {
        val map = JsonObject()
        map.addProperty("CardID", cardID)
        launchOnlyResult({
            val accountBean = repository.defaultGetAccount(map,"EMS_GetAccount")
            defUI.callObserve.value = CallObserveBean(4, accountBean)
        })
    }

}